import {reqWorkList,reqStoreList,reqAddStore,reqStoreDetail,reqStoreEdit,reqOrderList,reqUserPwd,reqOrderDow} from '@/api';

//login模块的仓库
const state = {
  //登录接口返回的token
  workInfo: JSON.parse(sessionStorage.getItem("WORKINFO")) || {},
  storeInfo: JSON.parse(sessionStorage.getItem("STOREINFO")) || [],
  storeDetail: JSON.parse(sessionStorage.getItem("STOREDETAIL")) || {},
  orderInfo: JSON.parse(sessionStorage.getItem("ORDERINFO")) || [],
  imgInfo: JSON.parse(sessionStorage.getItem("IMGINFO")) || {},
};
const mutations = {
  GETWORKINFO(state,workInfo) {
    state.workInfo = workInfo;
    sessionStorage.setItem("WORKINFO", JSON.stringify(workInfo));
  },
  GETSTOREINFO(state,storeInfo) {
    state.storeInfo = storeInfo;
    sessionStorage.setItem("STOREINFO", JSON.stringify(storeInfo));
  },
  GETSTOREDETAIL(state,storeDetail){
    state.storeDetail = storeDetail;
    sessionStorage.setItem("STOREDETAIL", JSON.stringify(storeDetail));
  },
  GETORDERINFO(state,orderInfo){
    state.orderInfo = orderInfo;
    sessionStorage.setItem("ORDERINFO", JSON.stringify(orderInfo));
  },
  GETORDERDOW(state,imgInfo){
    state.imgInfo = imgInfo;
    sessionStorage.setItem("IMGINFO", JSON.stringify(imgInfo));
  },
};
const actions = {
  //获取工作台数据
  async getWorkInfo({commit}){
    let result = await reqWorkList();
    if(result.code==200){
      commit('GETWORKINFO',result.data);
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //获取门店列表
  async getStoreInfo({commit}){
    let result = await reqStoreList();
    if(result.code==200){
      commit('GETSTOREINFO',result.data);
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //门店新增
  async addStore({commit},data){
    let result = await reqAddStore(data);
    if(result.code==200){
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //门店详情
  async getStoreDetail({commit},data){
    let result = await reqStoreDetail(data);
    if(result.code==200){
      commit('GETSTOREDETAIL',Object.assign(data,result.data));
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //门店修改
  async getStoreEdit({commit},data){
    let result = await reqStoreEdit(data);
    if(result.code==200){
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //订单列表
  async getOrderInfo({commit}){
    let result = await reqOrderList();
    if(result.code==200){
      commit('GETORDERINFO',result.data);
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //修改密码
  async setUserPwd({commit},data){
    let result = await reqUserPwd(data);
    if(result.code==200){
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
  //文件下载
  async getOrderDow({commit},data){
    let result = await reqOrderDow(data);
    if(result.code==200){
      commit('GETORDERDOW',result.data);
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
};
//计算属性
const getters = {};

//对外暴露store类的一个实例
export default {
  state,
  mutations,
  actions,
  getters,
};